<template>
  <div class="screen-right">
    <nanning-screen-border-box :bg-img="bgImg">
      <screen-title>查询列表</screen-title>
      <div class="screen-right-content">
        <nanning-screen-search @on-input-search="onInputSearch" @on-click-search="onClickSearch" placeholder-value="请输入公交线路"></nanning-screen-search>
        <div class="total-num">共{{totalCount}}条数据</div>
        <div class="loading-container" v-show="loadStatus">
          <screen-loading>加载中...</screen-loading>
        </div>
        <div class="query-list" v-show="queryList.length > 0 && !loadStatus">
          <div class="list-item" v-for="item in queryList" :key="item.segmentId" @click="chooseBusWay(item)">
            <div class="select-box">
              <el-checkbox v-model="item.select"></el-checkbox>
            </div>
            <div class="detail">
              <div class="bus-num">{{item.routeName}}</div>
              <div class="route">{{item.qdz || '无'}} ---> {{item.zdz || '无'}}</div>
            </div>
          </div>
          <screen-load-more :loadMoreVisible="loadMoreVisible" :loadMoreType="loadMoreType"></screen-load-more>
        </div>
        <div class="nodata-container" v-show="queryList.length === 0 && !loadStatus">
          <div class="nodata-img">
            <img src="../../../../assets/images/no-data.png" alt="">
          </div>
        </div>
      </div>
    </nanning-screen-border-box>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import { getAllRouteList } from 'api/situationAwareness/busWay/screenCenter'
import Bus from 'utils/eventBus'
enum LoadMoreType {
  none = 'none',
  loading = 'loading',
}
@Component({
  name: 'screen-right'
})

export default class ScreenRight extends Vue {
  private bgImg: string = require('assets/images/situational-awareness/security-left-border.png')
  private checkList: any[] = []
  private queryList: Array<any> = []
  private loadMoreVisible = false
  private loadMoreType: LoadMoreType = LoadMoreType.none
  private searchInputValue = ''
  private pageNo = 1
  private pageSize = 10
  private timestamp = Date.now().toString()
  private loadStatus = true
  private totalCount = 0

  created () {
    this.getAllRouteList()
  }

  mounted () {
    this.onReachBottom()
  }

  beforeDestroy () {
    this.removeReachBottom()
  }

  // 获取搜索框内容
  private onInputSearch (value: string) {
    this.searchInputValue = value
  }

  // 搜索框按钮回调
  private onClickSearch () {
    this.getAllRouteList()
  }

  // 获取公交线路列表
  private async getAllRouteList () {
    this.queryList = []
    this.loadMoreType = LoadMoreType.none
    this.loadMoreVisible = false
    this.timestamp = Date.now().toString()
    this.pageNo = 1
    this.loadStatus = true
    this.totalCount = 0
    const params = {
      pageIndex: this.pageNo,
      pageSize: this.pageSize,
      routeName: this.searchInputValue,
      dzcode: '',
      timestamp: this.timestamp
    }
    try {
      const res: any = await getAllRouteList(params)
      console.log('获取公交线路列表', res)
      this.totalCount = res.data.total || 0
      res.data.list.forEach((item: any) => {
        this.queryList.push({
          ...item,
          select: false
        })
      })
      this.loadMoreType = res.data.list.length === this.pageSize ? LoadMoreType.loading : LoadMoreType.none
      this.loadStatus = false
    } catch (err) {
      console.error('获取公交线路列表', err)
      this.loadMoreType = LoadMoreType.none
      this.queryList = []
      this.loadStatus = false
    }
  }

  // 下拉加载更多公交线路列表
  private async loadMoreAllRouteList () {
    const params = {
      pageIndex: this.pageNo,
      pageSize: this.pageSize,
      routeName: this.searchInputValue,
      dzcode: '',
      timestamp: this.timestamp
    }
    try {
      const res: any = await getAllRouteList(params)
      console.log('获取公交线路列表', res)
      res.data.list.forEach((item: any) => {
        this.queryList.push({
          ...item,
          select: false
        })
      })
      this.loadMoreType = res.data.list.length === this.pageSize ? LoadMoreType.loading : LoadMoreType.none
    } catch (err) {
      console.error('获取公交线路列表', err)
      this.loadMoreType = LoadMoreType.none
    }
  }

  // 触底检测
  private onReachBottom() {
    // @ts-ignore
    document.querySelector('.query-list').addEventListener('scroll', this.reachBottomCb.bind(this))
  }

  // 移除触底检测
  private removeReachBottom() {
    // @ts-ignore
    document.querySelector('.query-list').removeEventListener('scroll', this.reachBottomCb.bind(this))
  }

  // 触底检测回调函数
  private reachBottomCb(e: any) {
    const { scrollHeight, scrollTop, clientHeight } = e.target
    if (clientHeight + scrollTop >= scrollHeight) {
      this.loadMoreVisible = true
      if (this.loadMoreType === LoadMoreType.loading) {
        this.pageNo += 1
        this.loadMoreAllRouteList()
      }
    }
  }

  // 点击切换线路
  private chooseBusWay(busWay: any) {
    this.queryList.forEach((item: any) => {
      if (item.segmentId === busWay.segmentId) {
        if (!item.select) {
          item.select = true
          const { id: routeId, segmentId } = item
          Bus.$emit('onChangeBusWay', { routeId, segmentId })
        }
      } else {
        item.select = false
      }
    })
  }
}
</script>

<style lang="scss" scoped>
img {
  width: 100%;
  height: 100%;
  float: left;
}
.screen-right {
  width: 25.47%;
  height: 87.13%;
  position: absolute;
  z-index: 100;
  top: 0;
  right: 0;
  &-content {
    width: 100%;
    height: calc(100% - 50px);
    box-sizing: border-box;
    padding: 15px 24px 28px 22px;
    .total-num {
      width: 100%;
      height: 4em;
      line-height: 56px;
      font-size: 14px;
      font-family: 'PingFang Regular';
      font-weight: 400;
      color: rgba(255, 255, 255, 0.67);
      margin-top: 10px;
      text-indent: 20px;
    }
    .loading-container {
      width: 100%;
      height: calc(100% - 42px - 56px - 100px);
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .nodata-container {
      width: 100%;
      height: calc(100% - 42px - 56px - 100px);
      display: flex;
      align-items: center;
      justify-content: center;
      .nodata-img {
        width: 249px;
        height: 172px;
      }
    }
    .query-list {
      width: 100%;
      height: calc(100% - 42px - 18px - 28px);
      box-sizing: border-box;
      padding: 0 20px;
      overflow-y: auto;
      &::-webkit-scrollbar {
        /*滚动条整体样式*/
        width: 6px; /*高宽分别对应横竖滚动条的尺寸*/
        height: 1px;
      }
      &::-webkit-scrollbar-thumb {
        /*滚动条里面小方块*/
        border-radius: 10px;
        box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        background: #114da7;
      }
      &::-webkit-scrollbar-track {
        /*滚动条里面轨道*/
        box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        border-radius: 10px;
        background: #061836;
      }
      .list-item {
        width: 100%;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        padding: 14px 0px 15px;
        position: relative;
        cursor: pointer;
        .select-box {
          width: 18px;
          height: 18px;
          margin-right: 18px;
        }
        .detail {
          .bus-num {
            font-size: 18px;
            font-family: 'PingFang Regular';
            font-weight: 400;
            color: #fff;
            line-height: 28px;
          }
          .route {
            font-size: 16px;
            line-height: 26px;
            font-family: 'PingFang Regular';
            font-weight: 400;
            color: rgba(255, 255, 255, 0.67);
          }
        }
        & + .list-item {
          border-top: 1px solid #3c5fae;
        }
        &::after {
          content: '';
          position: absolute;
          width: 100%;
          height: 100%;
          left: 0;
          top: 0;
          display: none;
          background: rgba(0, 0, 0, 0.2);
        }
        &:active::after {
          display: block;
        }
      }
    }
  }
}
</style>
